import {
  Injectable
} from '@angular/core';
import {
  Http,
  Headers,
  RequestOptions,
  URLSearchParams
} from "@angular/http";
import {
  SafeUrlPipe
} from './pipe/safeUrl.pipe';
import {
  Observable
} from 'rxjs/observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
import {
  RouterModule,
  Routes
} from '@angular/router';
import { DialogService } from './service/dialog.service'


@Injectable()
export class ApiServiceService {
  host = "/api";
  // host = "http://192.168.11.97:7001";

  time: string;
  weekday: string;

  constructor(private http: Http, private dialogService: DialogService) {
  }

  getplatForm() {
    return this.http
      .get(this.host + `/config/`)
      .map(res => res.json());
  }

  saveConfig(data) {
    return this.http
      .put(this.host + "/config/", { conf: data })
      .map(res => res.json());
  }


  getAcceptor() {
    return this.http
      .get(this.host + '/eom/EBRAS')
      .map(res => res.json());
  }
  getPlaysys() {
    return this.http
      .get(this.host + '/eom/EBRBS')
      .map(res => res.json());
  }

  getTerminal() {
    return this.http
      .get(this.host + '/eom/EBRDT')
      .map(res => res.json());
  }

  getAllInfo(fonts) {

    return this.http
      .get(this.host + `/eom/${fonts}`)
      .map(res => {
        return res.json();
      })
  }


  getAcceptorStatus() {
    return this.http
      .get(this.host + `/omd/EBRASState`)
      .map(res => res.json());
  }

  getStation() {
    return this.http

      .get(this.host + "/eom/EBRST")
      .map(res => res.json());
  }



  updateoper(data) {
    return this.http.put(this.host + `/omd/EBDT`, { data: data }).map(res => res.json());
  }


  addEom(type, data) {
    return this.http
      .post(this.host + `/eom/${type}`, { data: data })
      .map(res => res.json());
  }
  doError(err) {
    console.log(err);
    this.dialogService.openSnackBar("调取接口失败!", 'OK')
  }

  reportEom(type, data) {
    return this.http.post(this.host + `/eom/upload/${type}`, { ids: data }).map(res => res.json());
  }

  reportEomStatus(type, data) {
    return this.http.post(this.host + `/eom/status/${type}`, { ids: data }).map(res => res.json());
  }

  deleteEom(type, ids: Array<any>) {

    return this.http
      .post(this.host + '/eom/delete/' + type, { ids: ids })
      .map(res => {
        return res.json()
      });
  }


  /** author: 林朝军   **/

  getAllTerminal(uid): Observable<any> {
    return this.http
      .get(this.host + '/eom/EBRAS')
      .map(res => res.json());
  }


  updateEom(type, data) {
    return this.http
      .put(this.host + `/eom/${type}`, { data: data })
      .map(res => res.json());
  }

  // 获取所有运维日志
  getLogs() {
    return this.http
      .get(this.host + "/omgLog")
      .map(res => res.json());
  }

  // 获取日志详情
  logDetail(id) {
    return this.http
      .get(this.host + "/omgLog/" + id)
      .map(res => res.json());
  }

  resDetail(id) {
    return this.http
      .get(this.host + "/omgLog/resDetail/" + id)
      .map(res => res.json());
  }

  // 获取播发记录
  getEBMLogs() {
    return this.http
      .get(this.host + "/ebmLog")
      .map(res => res.json());
  }

  /**
   * 上报播发记录
   * @param ids 上报的播放记录的id集合
   */
  reportPlayLog(ids: Array<String>) {
    return this.http
      .post(this.host + "/ebmLog", { ids })
      .map(res => res.json());
  }

  PlayRequest(formData: FormData) {
    return this.http
      .post(this.host + "/ebm/play", formData)
      .map(res => res.json());
  }

  stopEbm(EBMID) {
    return this.http
      .post(this.host + "/ebm/stop", {
        EBMID: EBMID
      })
      .map(res => res.json());
  }



  getStationStatus() {
    return this.http
      .get(this.host + `/omd/EBRSTInfo`)
      .map(res => res.json());
  }


  getPlaySystem() {
    return this.http
      .get(this.host + `/omd/EBRBSInfo`)
      .map(res => res.json());
  }

  getPlaySystemStatus() {
    return this.http
      .get(this.host + `/omd/EBRBSState`)
      .map(res => res.json());
  }

  getTerminalInfo() {
    return this.http
      .get(this.host + `/terminal`)
      .map(res => res.json());
  }

  reportInfo(id) {
    return this.http
      .post(this.host + `/ebmupload`, id)
      .map(res => res.json());
  }

  reportAllInfo() {
    return this.http
      .put(this.host + ``, {})
      .map(res => res.json());
  }

  setAutoReport() {
    return this.http
      .put(this.host + ``, {})
      .map(res => res.json());
  }

  sendPlayRequest(formData) {
    return this.http
      .post(this.host + `/ebmreq`, formData)
      .map(res => res.json());
  }
  getPlatformStatus() {
    return this.http
      .get(this.host + `/config`)
      .map(res => res.json());
  }
  setHeart() {
    return this.http
      .post(this.host + `/changeconfig`, { type: 'heart' })
      .map(res => res.json());
  }

  setSign() {
    return this.http
      .post(this.host + `/changeconfig`, { type: 'sign' })
      .map(res => res.json());
  }

  setDesign() {
    return this.http
      .post(this.host + `/changeconfig`, { type: 'checkSign' })
      .map(res => res.json());
  }

  setReport() {
    return this.http
      .post(this.host + `/changeconfig`, { type: 'auto_send' })
      .map(res => res.json());
  }

  /**
   * 改变平台状态
   * @param code
   */
  changeState(code) {
    return this.http
      .post(this.host + '/config/state', { code })
      .map(res => res.json())
  }

  addAccessNode(ip) {
    return this.http
      .post(this.host + '/accessnode', { ip })
      .map(res => res.json())
  }

  updateAccessNode(data) {
    return this.http
      .put(this.host + '/accessnode', data)
      .map(res => res.json())
  }

  deleteAccessNode(_id) {
    return this.http
      .delete(this.host + '/accessnode/' + _id)
      .map(res => res.json())
  }

}
